package org.ww.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.ww.order.vo.PayVo;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    /**
     * 以下配置可以使用alipay在配置文件中进行配置
     */
    //在支付宝创建的应用的id
    private   String app_id = "9021000141668918";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDDnMfomP9Wb0nqtUbQX+WTk5LKer7Zs8LnQPQTVmBd/f7kvDaf+YepOqP/iTjdNg09J4koQlMIDVVJhZthiinLqFfwMfTt1unMp/Uz1ecSN1hM7KX9Uhm/hmRlalKwfIICAjYghKvXVyBCpgy9g1x09XP82oby8ueJyDPI2zkp5z5oqY/Me/Xzvr7C5tuJkM1IUzjg0MDUvesIoPTx6yG+Dhtj0RizTzpanVfRCMI8hQg0k9Chh4Iy7BRvzhIUd7kywVu3u8FlQR/5KiHDaEQhMC2xMJ2j4qxUqwApoXRBpGXys76Cw3k2jsC+7pTxOuJaNr2xIr/ujgT0MKhcQKbnAgMBAAECggEAQeH3Lq8BKOJ4udVuUVZLI2ZLxWiMCRTTa0HkMvMfp+iZRf7iQwUvHN9ERxe3kEHZOaSsPGQCRS8Ab6/RQovfQdmkLKEWxlhhDG3xVxEPL4J2egomaeiSBYDFJacmzIxS2UxXYKJuoiuVEP2OQvIQXNAizaHoW8FWHIDSeC7l1OeXna3JVroc1nFWriVXnLoD/rrTYabCe4HyFf5eoAe5dm5l4S25UqMUQPp2Ny48bcXJXLD8AKKOOKPUwCD5VDvOK5bNv+APKodNlRfcGqZjMdwH8ovyR9HGfJufNJYOpYN0p7EERM6YrAEJGloY6deRVDunQ7nknSO5GyC1YI+H+QKBgQDrX3MR8OaZOYYn8qGJ7cVNK0sri09PhJelDPeIhygZVqxfNUWR30PTf9vbje7CNfQeVAUcfQSZbtXUYAk3XeYQGGtXohyRM9bjMxK9yL8sdnn3ZFdAN7auTUY5di18EdY66vce6Tel1Q5UgEs5JJIH0K84XQSc2HQ5c5HJGtDivQKBgQDUwVAcc+qWpwNOmXts+pDFolD9Z5LQkDkLH+BvyZIJpJvNVBGanC86mKmsNj7xofC70/s3jQ9NoWWlEWhPPwooaIGJ8275swn/NSi9GloQ+NUccRrz9EK+aPoZ3fHQvFjmZGtVmIP64FPun6D9UuZDL+yjD6dTDgZUsgqTZAi8cwKBgQDLnRi4boOuDZtGt5iS70P9S/SNSMhla7ibqaoBS5qewOIAwt7aICBWuIcDWbcv/uidy+VYDbaK3NzSCfovEWXSiq1hc/Xujr/Yb/hIgX8QAbXlE8J+5TN9eY/Eab1TVULUWStKbEodhUAOTaXFWyOQ6zCJO76fyIhSou184VvOqQKBgDWizpl9t6RnUnQcSXt958WLWDOqSt79nQPq81/7Ry/bKdQn35BE07tIBW5+9ntgQhp4JJsqmrMNf1yP+OYRlHP/dLjcgBu/QUPvmOejnxUsRj23Xf4mlDUArhybUX04Holmw5B+tLcomsBLygBTR+2kRVoOdjaAEKSFhhkGXRV1AoGAIbOvH+2T8kf46EAs5cZrJlfN/L2eRPKjTuLnDP/3UIC1xrVZNqpCXfH/5ijGlzreloy5W7AwrCDzVKZfoabAdSqpE/tAkljxKhW727Lr70V6r1uSoB5gtV57ysk1IPHUEtJuh7EB/qHw+5wl5CLftyRJ13+i4HalgRHA5785teA=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm2cOm3hwEghBJKlSVV46d9qY3PkAkPpNNXSDXT7vLq9XZIHKcEU6K5KCuNIeSuoAGcyrVf17U4Sox8N3zKRdo7xuPTQ1P6eyRCoAjDLd5dwXGfJpYshN+0FiY3GPLZ/8lCmTpj3pHrMrgakN/gcPQnWsovt96+/ZwH4ZeO2oBpujLTJ9l1CApFYA24iLNTege9jKLTlcKRSw181GQzGRLhcuQlYOjaKcduTY8nHqiFIfnqIgl3936MvFSJgQmAscvOeqAwUbvyRTPpAPmCMaENf5Lcxzn99PtcnsF+8ALagrV0MLY9p9e23JtcLCZdDG3oYX5quLlHDSXPQTw4t+gQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
//    private  String notify_url = "http://e4788137g4.zicp.fun/notify_url.jsp";
    private  String notify_url = "http://e4788137g4.zicp.fun//payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
//    private  String return_url ="http://e4788137g4.zicp.fun/return_url.jsp";
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "UTF-8";

    // 订单过期时间
    private String timeout_express = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);
//
//        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        //timeout_express是支付宝自动收单功能，多长时间后，交易通道关闭
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout_express +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
